package Wires.presentation;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.Viewer;

class FilePatternFilter extends ViewerFilter {
	String fileExtension;   
	public FilePatternFilter(String ext){
	   super();
	   fileExtension = ext;
	}
	   
	public boolean select(Viewer viewer, Object parentElement, Object element) {
	     IResource resource = null;
	     if (element instanceof IResource) {
	        resource = (IResource) element;
	     } else if (element instanceof IAdaptable) {
	        IAdaptable adaptable = (IAdaptable) element;
	        resource = (IResource) adaptable
	                    .getAdapter(IResource.class);
	     }
	     if (resource != null && !resource.isDerived()) {
	       if (resource.getType() != IResource.FILE) return true;
	       String extension = resource.getFileExtension();
	       if (extension == null) return true;          
	       if (extension.equals(fileExtension)) return true;        
	     }     
	     return false;      
	}
}